#include "lvgl.h"
#include "img_def.h"
#include "lv_common_function.h"
#include "fr_device_canfd.h"
 
 
static void event_handler(lv_event_t *e)
{
	lv_event_code_t event = lv_event_get_code(e);
	lv_obj_t *obj = lv_event_get_target(e);

	if(!lv_obj_is_valid(obj))
        return;
	if(event == LV_EVENT_KEY)
	{
		uint32_t *key = lv_event_get_param(e);
        switch(*key)
        {
        case KEY1_CODE:
				 
             break;
        case KEY3_CODE:
				 
             break;
        case KEY5_CODE: //return
            canfd_exit();
            user_jump_page(fr_list_page);
            break;
        }
		printf("key_code %d \n", *key);
	}
}

uint32_t dev_get_canid(void)
{
    // return  lv_rand(0,0xffff);;
    return get_canfd_id();
}

uint8_t dev_get_can_mode(void)
{
    // return lv_rand(0,1);
    return get_canfd_mode();
}

uint32_t dev_get_can_count(void)
{
    // return lv_rand(0,1024);
    return get_canfd_count();
}

void dev_get_can_raw(uint8_t *buff,uint16_t len)
{
    /*
    for(int i=0;i<len;i++)
    {
       buff[i] = lv_rand(0,1024)%255;
    }
    */
    get_canfd_value(buff, len);
}

static void page_del_cb(lv_event_t *e)
{
	if(e->user_data!=NULL)
	{
		lv_timer_del(e->user_data);
        e->user_data = NULL;
	}
}

static void refresh_timer_cb(lv_timer_t *t)
{
    lv_obj_t *parent = t->user_data;
    if(!lv_obj_is_valid(parent))
		return;
	 
	lv_label_set_text_fmt(lv_obj_get_child(parent,1),"Mode:%s",dev_get_can_mode()?"High":"Low");
	lv_label_set_text_fmt(lv_obj_get_child(parent,2),"FrameID:0x%04x",dev_get_canid());
	lv_label_set_text_fmt(lv_obj_get_child(parent,3),"Count:%d",dev_get_can_count());
	uint8_t can_data[64]={0};
	dev_get_can_raw(can_data,64);
	char buf[64*4]={0};
    uint8_t disp_len = dev_get_can_count();
    int i = 0;
    for(i=0;i<disp_len;i++)
    {
        snprintf(&buf[i*3],sizeof(buf),"%02x ",can_data[i]);
    }
    for (;i<64;i++) {
        snprintf(&buf[i*3],sizeof(buf),"   ");
    }
	lv_label_set_text(lv_obj_get_child(parent,5),buf);
}
void fr_can_page(lv_obj_t* parent,lv_point_t *top)
{
	uint8_t i;
    if(!lv_obj_is_valid(parent))
        return;
    UI_PARENT_INIT(parent);

    canfd_init();

//	lv_obj_t *obj_cont = lv_obj_create(parent);
//	lv_obj_set_size(obj_cont,LV_PCT(100),LV_PCT(100));
//	lv_obj_set_style_bg_color(obj_cont,lv_color_black(),0);
//	lv_obj_set_style_pad_all(obj_cont,0,0);
//	lv_obj_set_style_pad_bottom(obj_cont,10,0);
//	lv_obj_set_style_pad_top(obj_cont,10,0);
//	lv_obj_set_style_border_width(obj_cont,0,0);
//    lv_obj_set_scrollbar_mode(obj_cont, LV_SCROLLBAR_MODE_OFF);
//    //lv_obj_clear_flag(obj_cont, LV_OBJ_FLAG_SCROLLABLE);
//	lv_obj_set_pos(obj_cont,0,0);

	lv_obj_t* tile_label = lv_label_create(parent);
    lv_obj_align(tile_label,LV_ALIGN_TOP_MID,0,10);
    lv_obj_set_style_text_font(tile_label,LV_FONT_ARIALUNI_ALL_32PX,0);
    lv_obj_set_style_text_color(tile_label,lv_color_make(0xff, 0xff, 0xff),0);
    lv_label_set_text(tile_label,"CAN 收发测试");


    lv_obj_t* mode_label = lv_label_create(parent);
    lv_obj_set_style_text_font(mode_label,LV_FONT_SMALL_NUMBER_20PX,0);
    lv_obj_set_style_text_color(mode_label,lv_color_make(0xff, 0xff, 0xff),0);
    lv_label_set_text_fmt(mode_label,"Mode:%s",dev_get_can_mode()?"High":"Low");
    lv_obj_align(mode_label,LV_ALIGN_TOP_LEFT,10,60);


	lv_obj_t* id_label = lv_label_create(parent);
    lv_obj_set_style_text_font(id_label,LV_FONT_SMALL_NUMBER_20PX,0);
    lv_obj_set_style_text_color(id_label,lv_color_make(0xff, 0xff, 0xff),0);
    lv_label_set_text_fmt(id_label,"FrameID:0x%04x",dev_get_canid());
    lv_obj_align(id_label,LV_ALIGN_TOP_LEFT,10,100);

    lv_obj_t* count_label = lv_label_create(parent);
    lv_obj_set_style_text_font(count_label,LV_FONT_SMALL_NUMBER_20PX,0);
    lv_obj_set_style_text_color(count_label,lv_color_make(0xff, 0xff, 0xff),0);
    lv_label_set_text_fmt(count_label,"Count:%d",dev_get_can_count());
    lv_obj_align(count_label,LV_ALIGN_TOP_LEFT,10,140);

    char buf[64*4]={0};
	uint8_t can_data[64]={0};
	dev_get_can_raw(can_data,64);
    for(int i=0;i<64;i++)
    {
        snprintf(&buf[i*3],sizeof(buf),"%02x ",can_data[i]);
    }
    lv_obj_t* data_tile = lv_label_create(parent);
//    lv_obj_set_style_text_font(data_tile,LV_FONT_ARIALUNI_ALL_32PX,0);
	lv_obj_set_style_text_font(data_tile,LV_FONT_SMALL_NUMBER_20PX,0);
    lv_obj_set_style_text_color(data_tile,lv_color_make(0xff, 0xff, 0xff),0);
    lv_label_set_text(data_tile,"data(hex):");
    lv_obj_align(data_tile,LV_ALIGN_TOP_LEFT,180,60);
 
	
	lv_obj_t* data_label = lv_label_create(parent);
	lv_obj_set_style_text_font(data_label,LV_FONT_SMALL_NUMBER_20PX,0);
    lv_obj_set_style_text_color(data_label,lv_color_make(0xff, 0xff, 0xff),0);
	lv_obj_set_pos(data_label,180,100);
	lv_label_set_text(data_label,buf);
	lv_obj_set_width(data_label,LV_PCT(60));
	
	lv_timer_t *refresh_timer = lv_timer_create(refresh_timer_cb,50,parent);
	
	lv_obj_add_event_cb(data_label,page_del_cb,LV_EVENT_DELETE,(void*)refresh_timer);
	
	lv_obj_add_event_cb(parent,event_handler, LV_EVENT_KEY, NULL);
	
	
}
